package action;

import manager.UserManager;
import model.User;

import org.apache.struts2.convention.annotation.*;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.*;
import com.opensymphony.xwork2.ModelDriven;
/**
 * Struts action class for registering.
 *
 * @author Cylixstar
 *
 */
@SuppressWarnings("serial")
@Results({
		@Result(name="success",location="/registerSucc.jsp"),
		@Result(name="input",location="/register.jsp")
	})
@Action("register")
public class RegisterAction extends ActionSupport implements ModelDriven<User>{
	private User user = new User();
	
	@Override
	@Validations(
			requiredStrings= {
					@RequiredStringValidator(type = ValidatorType.SIMPLE,fieldName="username",
						message="Username must not be empty."),
					@RequiredStringValidator(type = ValidatorType.SIMPLE,fieldName="password",
							message="Password must not be empty."),
					@RequiredStringValidator(type = ValidatorType.SIMPLE,fieldName="nickname",
							message="Nickname must not be empty."),
					@RequiredStringValidator(type = ValidatorType.SIMPLE,fieldName="email",
							message="E-mail address is neccessary.")
			},
			regexFields = {
					@RegexFieldValidator(type = ValidatorType.SIMPLE,fieldName="username",expression="[\\w-]{1,25}", 
						message="The username can only consist of alphabetic ,numeric character, underline and hypen as longer as 25 characters."),
					@RegexFieldValidator(type = ValidatorType.SIMPLE,fieldName="password",expression="[\\w-]{6,25}", 
							message="The password can only consist of alphabetic ,numeric character, underline and hypen " 
								+ "as longer as 16 characters and at least 6 characters.")
			},
			stringLengthFields = {
					@StringLengthFieldValidator(type = ValidatorType.SIMPLE,
							fieldName = "nickname",
							maxLength = "10",
							message = "The nick name can be as long as 10 characters.")
			},
			emails = {
					@EmailValidator(type = ValidatorType.SIMPLE,
							fieldName = "email",
							message = "Correct e-mail address needed.")
			}
	)
	public String execute() throws Exception {
		UserManager.getUserManager().register(user);
		return SUCCESS;
	}
	
	public User getModel() {
		return user;
	}
	
	
}
